package com.xuecheng.learning.service.impl;

import com.alibaba.fastjson.JSON;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.learning.config.PayNotifyConfig;
import com.xuecheng.learning.service.MyCourseTablesService;
import com.xuecheng.messagesdk.model.po.MqMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 接收消息通知处理类
 */
@Service
@Slf4j
public class ReceivePayNotifyServiceImpl {

    @Autowired
    private MyCourseTablesService myCourseTablesService;

    @RabbitListener(queues = PayNotifyConfig.PAYNOTIFY_QUEUE)
    public void receive(Message message){

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
           e.printStackTrace();
        }

        //解析消息
        byte[] body = message.getBody();
        String jsonString = new String(body);

        //转成对象
        MqMessage mqMessage = JSON.parseObject(jsonString, MqMessage.class);

        //解析消息内容
        String chooseCourseId = mqMessage.getBusinessKey1();//选课ID
        String orderType = mqMessage.getBusinessKey2();//订单类型

        //学习中心服务只要购买课程类的支付订单的结果
        if(orderType.equals("60201")){
            //根据消息内容，更新选课记录表(xc_choose_course)，像我的课程表插入数据(xc_course_tables)
            boolean b = myCourseTablesService.saveChooseCourseSuccess(chooseCourseId);
            if(!b){
                XueChengPlusException.cast("保存选课记录状态失败");
            }
        }
    }

}
